<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <form class="layui-form">
                <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">配送任务编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="distCode" class="layui-input" type="text" placeholder="配送任务编号"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">状态：</label>
                                    <div class="layui-input-inline">
                                        <select name="status" lay-filter="">
                                            <option value="">状态</option>
                                            <option value="0">待配货</option>
                                            <option value="1">配货中</option>
                                            <option value="2">配货完成</option>
                                        </select>
                                    </div>
                                </div>

                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">创建时间：</label>
                                    <div class="layui-input-inline">
                                        <input  id="pTime" class="layui-input date-icon" style="width: 200px;" type="text" placeholder="选择创建时间" autocomplete="off" readonly/>
                                    </div>
                                    <input name="startCreateTime" id="sTime" style="display:none"/>
                                    <input name="endCreateTime" id="eTime" style="display:none"/>
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button type="button" class="layui-btn icon-btn color-green" lay-filter="cloudDistributionSearch" lay-submit>查询</button>
                                    <button type="reset" class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            </form>
            <!-- 表格 -->
            <table class="layui-table" id="cloudDistributionTable" lay-filter="cloudDistributionTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="cloudDistributionTableBar">
    <a class="layui-btn layui-btn-xs" lay-event="detailsBtn">详情</a>
</script>

<!-- 详情弹窗 -->
<script type="text/html" id="detailsForm">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form" action="" lay-filter="detailsForm">
                    <div class="layui-form-item">
                        <table class="layui-table" id="detailsFormTable" name="detailsFormTable" lay-filter="detailsFormTable"></table>
                    </div>
                    <div class="layui-form-item">
                        <table class="layui-table" id="distributionDetailsFormTable" name="distributionDetailsFormTable" lay-filter="distributionDetailsFormTable"></table>
                    </div>
                </form>
            </div>
        </div>
    </div>
</script>


<!-- 配货详情弹窗 -->
<!-- distributionDetailsForm -->
<script type="text/html" id="distributionDetailsForm">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form" action="" lay-filter="distributionDetailsForm">
                    <div class="layui-form-item">
                        <table class="layui-table" id="distributionDetailsTableOne" name="distributionDetailsTableOne" lay-filter="distributionDetailsTableOne"></table>
                    </div>
                    <div class="layui-form-item">
                        <table class="layui-table" id="distributionDetailsTableTwo" name="distributionDetailsTableTwo" lay-filter="distributionDetailsTableTwo"></table>
                    </div>
                </form>
            </div>
        </div>
    </div>
</script>



<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laydate = layui.laydate;


        // 渲染表格
        var insTb = table.render({
            elem: '#cloudDistributionTable',
            url: config.base + 'enterprisecab/order/admin/cloudDistribution/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'}
                ,{field: 'distCode', sort: true, title: '配货任务编号'}
                ,{field: 'demandCode',title: '所属配货订单'}
                ,{field: 'customerName',title: '客户名称'}
                ,{field: 'customerConcat', title: '客户联系人'}
                ,{field: 'skuNum',title: '配货数量'}
                ,{field: 'address', title: '所在地区'}
                ,{field: 'enterpriseName',title: '所属服务商'}
                ,{field: 'createTime', sort: true, title: '提交时间'}
                ,{field: 'statusDesc',title: '状态'}
                ,{field: 'distUser',title: '配货员'}
                ,{align: 'center', toolbar: '#cloudDistributionTableBar', title: '操作'}
            ]],           
        });

        //渲染下拉框
        form.render('select');

        // 搜索
        form.on('submit(cloudDistributionSearch)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });;

        // 工具条点击事件
        table.on('tool(cloudDistributionTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detailsBtn') { //详情
                showDetailsForm(data)
            } 
        });

        //时间范围
        laydate.render({
            elem: '#pTime',
            type: 'date',
            range: true,
            done: function(value, date, endDate){
               if(value){
                   let dateValue = value.split(" - ");
                   $("input[name='startCreateTime']").val(dateValue[0]+" 00:00:00");
                   $("input[name='endCreateTime']").val(dateValue[1]+" 23:59:59");
               }else {
                   $("input[name='startCreateTime']").val("");
                   $("input[name='endCreateTime']").val("");
               }
            }
        });

        //监听排序
        table.on('sort(cloudDistributionTable)', function(obj){
            table.reload('cloudDistributionTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 显示详情
        // detailsForm
        function showDetailsForm(_data){
            admin.open({
                type: 1,
                area: ['1450px','800px'],
                offset: '65px',
                title: '详情',
                content: $('#detailsForm').html(),
                success: function () {
                    // 渲染表格
                    var detailsFormTb = table.render({
                        elem: '#detailsFormTable',
                        data:[_data],
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'deliveryTaskSukNum', title: '配货任务编号'}
                            ,{field: 'demandCode', title: '所属配货订单'}
                            ,{field: 'stockNum', title: '物料数量'}
                            ,{field: '', title: '订单货值'}
                            ,{field: 'creatorName', title: '创建人'}
                            ,{field: 'statusDesc', title: '状态'}
                            ,{field: 'distUser', title: '配货员'}
                            ,{field: 'claimTime', title: '认领时间'}
                            ,{field: 'confirmTime', title: '完成时间'}
                        ]],           
                    });

                    var distributionDetailsFormTb = table.render({
                        elem: '#distributionDetailsFormTable',
                        url: config.base + 'enterprisecab/order/admin/cloudDistribution/getDistributionDetailList?distId='+_data.id,
                        cellMinWidth: 100,
                        cols: [[
                            {field: '', title: '序号'}
                            ,{field: 'skuCode', title: '物料编号'}
                            ,{field: 'productName', title: '物料系列名称'}
                            ,{field: 'productSpecCode', title: '规格'}  // 有两个物料规格 productSpecName productSpecCode（下同）
                            ,{field: 'unitName', title: '单位'}
                            ,{field: 'price', title: '单价'}
                            ,{field: 'curentStock', title: '仓库库存'}
                            ,{field: 'statusDesc', title: '状态'}
                            ,{field: 'deliveryNum', title: '实配数'}
                            ,{align: 'center', toolbar: '#cloudDistributionTableBar', title: '操作'}
                        ]],           
                    });

                    // 工具条点击事件
                    table.on('tool(distributionDetailsFormTable)', function (obj) {
                        var data = obj.data;

                            data.deliveryTaskSukNum = _data.deliveryTaskSukNum
                            data.demandCode = _data.demandCode
                            data.distUser = _data.distUser
                            data.confirmTime = _data.confirmTime
                            data.distId = _data.id

                            

                        if (obj.event === 'detailsBtn') { //详情
                            showDistributionDetailsForm(data)
                        } 
                    });
                }
            })
        }
        

        // 配送详情
        // distributionDetailsForm
        function showDistributionDetailsForm(_data){
            console.log(_data)
            admin.open({
                type: 1,
                area: ['1400px','700px'],
                offset: '85px',
                title: '配送详情',
                content: $('#distributionDetailsForm').html(),
                success: function () {
                    // 渲染表格
                    var distributionDetailsTableTb1 = table.render({
                        elem: '#distributionDetailsTableOne',
                        data:[_data],
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'deliveryTaskSukNum', title: '配货任务编号'}
                            ,{field: 'demandCode', title: '所属配货订单'}
                            ,{field: 'skuCode', title: '物料编号'}
                            ,{field: 'productName', title: '物料系列名称'}
                            ,{field: 'productSpecCode', title: '规格'} 
                            ,{field: 'unitName', title: '单位'}
                            ,{field: 'curentStock', title: '仓库库存'}
                            ,{field: 'deliveryNum', title: '配货数(按最小单位)'}
                            ,{field: 'distUser', title: '配货员'}
                            ,{field: 'claimTime', title: '认领时间'}
                            ,{field: 'confirmTime', title: '完成时间'}

                        ]],           
                    });

                    var distributionDetailsTableTb2 = table.render({
                        elem: '#distributionDetailsTableTwo',
                        url: config.base + 'enterprisecab/order/admin/cloudDistribution/getDistributionDetailList?distId='+_data.distId+"&skuCode="+_data.skuCode,
                        cellMinWidth: 100,
                        cols: [[
                            {field: '', title: '序号'}
                            ,{field: 'packLabelCode', title: '包装编码'}
                            ,{field: 'productName', title: '物料系列名称'}
                            ,{field: 'packSpecCode', title: '包装规格'}
                            ,{field: '', title: '包装单位'}
                            ,{field: 'unitName', title: '最小单位库存数'}
                            ,{field: 'deliveryNum', title: '配货数(最小单位:支)'}
                        ]],           
                    });
               
                }
            })
        }
    });
</script>